#!/bin/bash

# 服务启动脚本（在容器内部执行）

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 加载配置
source /app/config/startup/services.conf

echo -e "${BLUE}启动Git环境服务...${NC}"

# 创建日志目录
mkdir -p $PG_LOG $REDIS_LOG $CELERY_LOG $LOG_DIR/app

# 设置权限
chown -R postgres:postgres /var/lib/postgresql
chown -R redis:redis /var/lib/redis

# 启动PostgreSQL
echo -e "${BLUE}启动PostgreSQL...${NC}"
if [ ! -f "$POSTGRES_PID" ]; then
    service postgresql start

    # 等待PostgreSQL启动
    sleep 5

    # 初始化数据库
    if [ -f "/app/config/startup/init-db.sql" ]; then
        echo -e "${BLUE}初始化数据库...${NC}"
        sudo -u postgres psql -f /app/config/startup/init-db.sql || true
    fi

    # 创建用户
    if [ -f "/app/config/startup/create-user.sql" ]; then
        echo -e "${BLUE}创建数据库用户...${NC}"
        sudo -u postgres psql -f /app/config/startup/create-user.sql || true
    fi

    echo -e "${GREEN}PostgreSQL启动成功${NC}"
else
    echo -e "${YELLOW}PostgreSQL已在运行${NC}"
fi

# 启动Redis
echo -e "${BLUE}启动Redis...${NC}"
if [ ! -f "$REDIS_PID" ]; then
    redis-server $REDIS_CONF --daemonize yes
    echo -e "${GREEN}Redis启动成功${NC}"
else
    echo -e "${YELLOW}Redis已在运行${NC}"
fi

# 等待数据库服务就绪
echo -e "${BLUE}等待数据库服务就绪...${NC}"
sleep 3

# 启动FastAPI后端服务
echo -e "${BLUE}启动FastAPI后端服务...${NC}"
if [ -f "$FASTAPI_PID" ]; then
    rm -f $FASTAPI_PID
fi

cd $BACKEND_DIR
if [ -f "requirements.txt" ]; then
    source /opt/venv/bin/activate
    pip install -r requirements.txt
fi

# 启动FastAPI服务器
nohup uvicorn app.main:app --host 0.0.0.0 --port 7001 --reload > $LOG_DIR/app/fastapi.log 2>&1 &
echo $! > $FASTAPI_PID
echo -e "${GREEN}FastAPI启动成功${NC}"

# 启动Celery Worker
echo -e "${BLUE}启动Celery Worker...${NC}"
if [ -f "$CELERY_WORKER_PID" ]; then
    rm -f $CELERY_WORKER_PID
fi

cd $BACKEND_DIR
nohup celery -A app.celery worker --loglevel=info --concurrency=2 > $CELERY_LOG/celery-worker.log 2>&1 &
echo $! > $CELERY_WORKER_PID
echo -e "${GREEN}Celery Worker启动成功${NC}"

# 启动Celery Beat
echo -e "${BLUE}启动Celery Beat...${NC}"
if [ -f "$CELERY_BEAT_PID" ]; then
    rm -f $CELERY_BEAT_PID
fi

cd $BACKEND_DIR
nohup celery -A app.celery beat --loglevel=info --schedule=/app/logs/celery/celerybeat-schedule > $CELERY_LOG/celery-beat.log 2>&1 &
echo $! > $CELERY_BEAT_PID
echo -e "${GREEN}Celery Beat启动成功${NC}"

# 启动Vue.js前端服务
echo -e "${BLUE}启动Vue.js前端服务...${NC}"
if [ -f "$VUEJS_PID" ]; then
    rm -f $VUEJS_PID
fi

cd $FRONTEND_DIR
if [ -f "package.json" ]; then
    npm install
    nohup npm run dev > $LOG_DIR/app/vuejs.log 2>&1 &
    echo $! > $VUEJS_PID
    echo -e "${GREEN}Vue.js启动成功${NC}"
else
    echo -e "${YELLOW}前端代码不存在，跳过Vue.js启动${NC}"
fi

# 等待所有服务启动
echo -e "${BLUE}等待所有服务启动完成...${NC}"
sleep 10

echo -e "${GREEN}所有服务启动完成!${NC}"

# 显示服务状态
echo -e "${BLUE}=== 服务状态 ===${NC}"
echo -e "${GREEN}PostgreSQL: 运行在端口5432${NC}"
echo -e "${GREEN}Redis: 运行在端口6379${NC}"
echo -e "${GREEN}FastAPI: 运行在端口7001${NC}"
echo -e "${GREEN}Vue.js: 运行在端口8080${NC}"
echo -e "${GREEN}Celery Worker: 运行中${NC}"
echo -e "${GREEN}Celery Beat: 运行中${NC}"